package scala.cookbook.akka

import akka.actor._

/**
  * Created by baiwf on 2017/5/13.
  */

case object ActNormalMsg

case object TryToFindSolution

case object BadGuysMakMeAngry

class DavidBanner extends Actor {

  import context._

  def angryState: Receive = {
    case ActNormalMsg => println("back to normal"); become(normalState)
  }

  //在此状态下，收到相应消息，则自动被调用!
  def normalState: Receive = {
    case TryToFindSolution => println("enter trytofind")
    case BadGuysMakMeAngry =>
      println("enter badguys")
      become(angryState)
  }

  override def receive: Receive = {
    case BadGuysMakMeAngry => become(angryState)
    case ActNormalMsg => become(normalState)
  }
}

object BecomeToSwitchState extends App {
  val sys = ActorSystem("becomeex")
  val d = sys.actorOf(Props[DavidBanner], name = "cs")

  d ! ActNormalMsg
  d ! TryToFindSolution //注意在trytofind状态下并没有进行become切换
  d ! BadGuysMakMeAngry

  d ! ActNormalMsg

  Thread.sleep(1000)
  sys.shutdown()
}
